/* -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-
vim: ts=4 noet ai */

/**
	ScrollMeter -- Measure how far you scrolled today

	Copyright © 2016  Desktopd Project

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as
	published by the Free Software Foundation, either version 3 of the
	License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

	@license GPL-3.0+
	@file
*/

'use strict';

// Script on the panel
((w, p, d) => {
	const $prefButton = d.getElementById ('prefButton');
	const $domains = d.getElementById ('domains');
	
	$prefButton.addEventListener ('click', ev => {
		p.emit ('openPreferences', true);
	});
	
	p.on ('show', ([domains, isPrivate, ratio]) => {
		Array.from ($domains.children).forEach (el => el.remove ());
		
		Object.getOwnPropertyNames (domains).forEach (domain => {
			const el = d.createElement ('li');
			const label = el.appendChild (d.createElement ('strong'));
			const info = isPrivate ? ' (private)' : '';
			label.appendChild (d.createTextNode (domain + info));
			
			const number = (domains[domain] * ratio).toPrecision (3).split ('e');
			
			el.appendChild (d.createTextNode (' ' + number[0]));
			if (number.length > 1) {
				el.appendChild (d.createTextNode (' × 10'));
				const sup = el.appendChild (d.createElement ('sup'));
				sup.appendChild (d.createTextNode (+number[1]));
			}
			
			$domains.appendChild (el);
		});
	});
}) (this, addon.port, document);

